<?php
// +----------------------------------------------------------------------
// | 系统维护
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: MaintenanceAction.class.php 119 2011-05-13 08:05:33Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class MaintenanceAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	public function cacheManager() {

		/* 检查目录权限 */
		$path  =  ROOT_PATH . STATIC_CACHES_DIR;
		$mask  =  file_mode_info($path);
		if ($mask === false) {
			$warning = sprintf(L('dir_not_exist'), $path);
			$this->assign('warning', $warning);
		} elseif ($mask != 15) {
			$warning = sprintf(L('dir_priv'), $path) . '<br/>';
			if (($mask&1) < 1) {
				$warning .= L('cannot_read') . '&nbsp;&nbsp;';
			}
			if (($mask&2) < 1) {
				$warning .= L('cannot_write') . '&nbsp;&nbsp;';
			}
			if (($mask&4) < 1) {
				$warning .= L('cannot_add') . '&nbsp;&nbsp;';
			}
			if (($mask&8) < 1) {
				$warning .= L('cannot_modify');
			}
			$this->assign('warning', $warning);
		} else {
		    /* 获取文件列表 */
			$real_list = array();
			$folder = opendir($path);
			while ($file = readdir($folder)) {
				if (strpos($file,'.php') !== false && $file !== 'index.php') {
					$real_list[] = $file;
				}
			}
			natsort($real_list);
		}
		
		$filedesc = array(
			'calendar'          => L('m_calendar'),
			'category_isshow'   => L('m_category_isshow'),
			'category'          => L('m_category'),
			'cat_option'        => L('m_cat_option'),
			'comment'		    => L('m_comment'),
			'links'             => L('m_links'),
			'posts'             => L('m_posts'),
			'system_config'     => L('m_system_config'),
			'tag'               => L('m_tag'),
			'statistics'        => L('m_statistics'),
		);
	
		for($i=0; $i<count($real_list); $i++) {
			if($real_list[$i] != "." && $real_list[$i] != "..") {
				$list[] = array('file_name'=>$real_list[$i],'add_time'=>strftime("%Y-%m-%d %X",filemtime($path.$real_list[$i])),'file_size'=>num_bitunit(filesize($path.$real_list[$i])));
			}
		}
		foreach ($list as $k=>$v) {
			foreach ($filedesc as $ks=>$vs) {
				if ($v['file_name'] == $ks."_static.php") {
					 $list[$k]['name'] = $vs;
					 $list[$k]['n'] = $ks;
				}
			}
		}		
		//dump($list);
		$this->assign('ur_here',L('cacheManager'));
		$this->assign('list',$list);
		$this->display();	
	}
	
	public function cacheDetails() {
		$name = $_GET['n'];	
		$name = in_array($name, array('calendar', 'category', 'cat_option', 'category_isshow', 'comment', 'links', 'posts', 'system_config','tag','statistics')) ? $name : '';
		if ($name) {
			$path  =  ROOT_PATH . STATIC_CACHES_DIR;
			$filepath = $path.$name.'_static.php';
			if(is_file($filepath)) {
				$fp = fopen($filepath,'rb');
				$cachedata = fread($fp,filesize($filepath));
				$cachedata = str_replace("<?php\n//nblog cache file",'',$cachedata);
				$cachedata = str_replace("\n\nif (!defined('IN_BLOG')) exit('Hacking attempt');",'',$cachedata);
				$cachedata = str_replace("\n?>",'',$cachedata);	
				$cachedata = highlight_code($cachedata);
			} else {		
				$this->error(L('no_cache_file'));
			}
		} else {		
			$this->error(L('no_cache_file'));
		}
		
		$this->assign('action_link', array('href'=>__URL__.'/cacheManager/', 'text' => L('cacheManager')));
		$this->assign('ur_here',L('see_cache').$name.L('see_cache_'));
		$this->assign('cachedata',$cachedata);
		$this->assign('name',$name);
		$this->display();
	}
	
	public function reCache() {
		
		$name	=	$_REQUEST['n'];
		import("@.ORG.StaticCaches");
		$cache = new StaticCaches();
		
		if (in_array($name, array('calendar', 'category', 'cat_option', 'category_isshow', 'comment', 'links', 'posts', 'system_config','tag','statistics'))) {
		    if ($name == 'system_config') {
				clear_all_files();
			}
			$cache->cacheLoad($name);
			set_log('rebulid(StaticCache, Id: '.$name.')');
			redirect(__URL__.'/cacheDetails/n/'.$name);	
		} else {
			$this->error(L('illegal'));
		}	
	}

	public function reAllCache() {
		if ($_POST['act']) {
			clear_all_files();
			import("@.ORG.StaticCaches");
			$cache = new StaticCaches();
			$cache->cacheLoad();
			//create_static_caches();
			set_log('rebuild(StaticCache, All)');
			$this->assign('jumpUrl',__URL__.'/cacheManager/');
			$this->success (L('success'));
		} else {
			$this->error (L('illegal'));
		}
	}

	//清除缓存,包括静态缓存和编译的模板缓存
	public function clearCache() {
		$count = clear_all_files();
		set_log('clearCache(All: '.$count.')');
		$this->assign('jumpUrl',__APP__);
		$this->success(L('clear_all_files'));
	}
	
	public function rebuild() {
	
		$s1 = !empty($_REQUEST['s1']) ? L('rebuild_2') : L('rebuild_');
		$s2 = !empty($_REQUEST['s2']) ? L('rebuild_2') : L('rebuild_');
		
		if ($_REQUEST['act'] == 'posts') {	
			rebuild_posts();
			clear_compiled_files();
			set_log('rebuild(Posts)');
			redirect(__URL__.'/rebuild/s1/1');
		}
		
		if ($_REQUEST['act'] == 'statistics') {
			import("@.ORG.StaticCaches");
			$cache = new StaticCaches();
			$cache->cacheLoad('statistics');
			clear_compiled_files();
			set_log('rebuild(Statistics)');
			redirect(__URL__.'/rebuild/s2/1');	
		}	
		
		$this->assign('ur_here',L('rebuild'));
		$this->assign('s1',$s1);
		$this->assign('s2',$s2);
		$this->display();	
	}
	
}

?>